import dayjs from "dayjs";
import path from "path";
import iconv from 'iconv-lite';
import { DracoEncoderModule } from 'assets/draco_encoder.js'
import { DracoDecoderModule } from 'assets/draco_encoder.js'


export const trimPro = (data: string): string => {
    const cleanedData = data.replace(/\s+/g, '').replace(/[\r\n]+/g, '');
    return cleanedData;
  };

  export const resolvePath = (paths: string) => {
    return path.resolve(__dirname, paths);
  };
  
  export const getDayJS = (end:string) => {
    const formaDate = dayjs().format('YYYYMMDDHHmmssSSS') + end;
    return formaDate;
  };
  export const contentTypes = [
    'model/vnd.collada+xml',
    'application/vnd.autodesk.fbx',
    'application/vnd.ms-pki.stl',
    'image/x-3ds',
    'model/ply',
    'model/x3d+xml',
    'application/dxf',
    'text/plain',
    'model/obj',
    'model/gltf-binary',
    'model/gltf+json',
    'application/octet-stream'
  ];

  export function parseChineseText(input:string, encoding) {
    const endName = input.split('.')[input.split('.').length - 1];
    const buffer = Buffer.from(input, encoding);
    const decodedString = iconv.decode(buffer, 'utf-8'); // 将输入字符串从指定编码解码为 UTF-8
    const chineseWithNumberRegex = /[\u4e00-\u9fa5\d]+/g;
    const matches = decodedString.match(chineseWithNumberRegex);
  
    if (matches && matches.length > 0) {
      return matches.join('') + endName;
    }
  
    return '';
  }

//   export function encoder3D(){
//     const mesh = {
//       indices : new Uint32Array(indices),
//       vertices : new Float32Array(vertices),
//       normals : new Float32Array(normals)
//     };
    
//     const encoderModule = DracoEncoderModule();
//     const encoder = new encoderModule.Encoder();
//     const meshBuilder = new encoderModule.MeshBuilder();
//     const dracoMesh = new encoderModule.Mesh();
    
//     const numFaces = mesh.indices.length / 3;
//     const numPoints = mesh.vertices.length;
//     meshBuilder.AddFacesToMesh(dracoMesh, numFaces, mesh.indices);
    
//     meshBuilder.AddFloatAttributeToMesh(dracoMesh, encoderModule.POSITION,
//       numPoints, 3, mesh.vertices);
//     if (mesh.hasOwnProperty('normals')) {
//       meshBuilder.AddFloatAttributeToMesh(
//         dracoMesh, encoderModule.NORMAL, numPoints, 3, mesh.normals);
//     }
//     if (mesh.hasOwnProperty('colors')) {
//       meshBuilder.AddFloatAttributeToMesh(
//         dracoMesh, encoderModule.COLOR, numPoints, 3, mesh.colors);
//     }
//     if (mesh.hasOwnProperty('texcoords')) {
//       meshBuilder.AddFloatAttributeToMesh(
//         dracoMesh, encoderModule.TEX_COORD, numPoints, 3, mesh.texcoords);
//     }
    
//     if (method === "edgebreaker") {
//       encoder.SetEncodingMethod(encoderModule.MESH_EDGEBREAKER_ENCODING);
//     } else if (method === "sequential") {
//       encoder.SetEncodingMethod(encoderModule.MESH_SEQUENTIAL_ENCODING);
//     }
    
//     const encodedData = new encoderModule.DracoInt8Array();
//     // Use default encoding setting.
//     const encodedLen = encoder.EncodeMeshToDracoBuffer(dracoMesh,
//                                                        encodedData);
//     encoderModule.destroy(dracoMesh);
//     encoderModule.destroy(encoder);
//     encoderModule.destroy(meshBuilder);
    
//     return encodedLen;
//   }

//   export function decoder3D(byteArray){
// // Create the Draco decoder.
// const decoderModule = DracoDecoderModule();
// const buffer = new decoderModule.DecoderBuffer();
// buffer.Init(byteArray, byteArray.length);

// // Create a buffer to hold the encoded data.
// const decoder = new decoderModule.Decoder();
// const geometryType = decoder.GetEncodedGeometryType(buffer);

// // Decode the encoded geometry.
// let outputGeometry;
// let status;
// if (geometryType == decoderModule.TRIANGULAR_MESH) {
//   outputGeometry = new decoderModule.Mesh();
//   status = decoder.DecodeBufferToMesh(buffer, outputGeometry);
// } else {
//   outputGeometry = new decoderModule.PointCloud();
//   status = decoder.DecodeBufferToPointCloud(buffer, outputGeometry);
// }

// // You must explicitly delete objects created from the DracoDecoderModule
// // or Decoder.
// decoderModule.destroy(outputGeometry);
// decoderModule.destroy(decoder);
// decoderModule.destroy(buffer);
//   }

//   decoder3D(encoder3D())